<template>
  <div>
    <!-- 顶部面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <!-- 搜索文本输入框 -->
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search" @click="searchOrder"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="OrdersList" border stripe>
        <!-- 订单列表数据 -->
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="90px">
        </el-table-column>
        <el-table-column prop="address" label="是否付款" width="90px">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.order_pay==='0'" type="danger">未付款</el-tag>
            <el-tag v-else type="success">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="100px">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="200px">
          <template slot-scope="scope">
            {{scope.row.create_time|dateFormat}}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" width="150px">
          <template>
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog"></el-button>
            <el-button type="success" icon="el-icon-location-outline" size="mini" @click="showProgressDialog"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total" background>
      </el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="editDialogVisible" width="50%" @close="addressDialogClosed">
      <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="addressForm.address1" :props="{ expandTrigger: 'hover' }"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
        <span>由于后台没有这个数据接口，所以次服务暂不开发！</span>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddress">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <span>由于后台没有这个数据接口，所以此功能暂不开发！</span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata'
export default {
  name: 'MyOrder',
  data() {
    return {
      // 查询参数
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 10,
        // 用户ID
        user_id: '',
        // 支付状态
        pay_sataus: '',
        // 是否发货
        is_send: '',
        // 标题
        order_fapiao_title: '',
        // 公司名称
        order_fapiao_company: '',
        // 发票内容
        order_fapiao_content: '',
        // 发货地址
        consignee_addr: ''
      },
      // 订单列表数据
      OrdersList: [],
      // 总页数
      total: 0,
      // 控制修改地址对话框的显示与隐藏
      editDialogVisible: false,
      // 修改地址对话框数据
      addressForm: {
        address1: [],
        address2: ''
      },
      // 修改地址对话框验证规则
      addressFormRules: {
        address1: [{ required: true, message: '请选择省区县', trigger: 'blur' }],
        address2: [{ required: true, message: '请填写详细地址', trigger: 'blur' }]
      },
      cityData,
      // 控制物流对话框的显示与隐藏
      progressVisible: false
    }
  },
  created() {
    // 获取订单数据列表
    this.getOrdersList()
  },
  methods: {
    // 获取订单数据列表
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取订单列表数据失败！')
      this.OrdersList = res.data.goods
      this.total = res.data.total
    },
    // 每页条数改变函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrdersList()
    },
    // 当前页面改变函数
    handleCurrentChange(newCurrent) {
      this.queryInfo.pagenum = newCurrent
      this.getOrdersList()
    },
    // 搜索的点击事件
    searchOrder() {},
    // 修改对话框关闭事件
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    // 修改对话框的显示事件
    showEditDialog() {
      this.editDialogVisible = true
    },
    // 修改对话框的确定事件
    editAddress() {
      this.$refs.addressFormRef.validate(async valid => {
        if (!valid) return this.$message.error('请填写必要的表单项')
        // const {data:res} = await this.$http.
        // this.addressForm.address1.forEach(item => {
        //   this.queryInfo.consignee_addr += item
        // })
        // this.queryInfo.consignee_addr += this.addressForm.address2
        // this.getOrdersList()
        this.editDialogVisible = false
        this.$message.success('修改地址成功！')
      })
    },
    // 物流进度按钮点击事件
    async showProgressDialog() {
      this.progressVisible = true
      // const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      // if (res.meta.status !== 200) return this.$message.error('获取物流信息失败！')
      // console.log(res.data)
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
